<?php
load()->func('tpl');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$_W['page']['title'] = '优惠券管理';
if ($operation == 'display') {

    $size = 7;
    $page = !empty($_GPC['page']) ? $_GPC['page'] : 1;
    $sql = "select * from " . tablename('tg_favorable') . " where uniacid='{$_W['uniacid']}' LIMIT " . ($page - 1) * $size . " , " . $size;
    $list = pdo_fetchall($sql);

    $total = pdo_fetchall("select id from " . tablename('tg_favorable') . " WHERE uniacid = {$_W['uniacid']}");
    $total = count($total);

    $pager = pagination($total, $page, $size);

    include wl_template('goods/coupon');
} elseif ($operation == 'on_shelves') {

    $size = 7;
    $page = !empty($_GPC['page']) ? $_GPC['page'] : 1;
    $sql = "select * from " . tablename('tg_favorable') . " where uniacid = '{$_W['uniacid']}' and isshow = 1 LIMIT " . ($page - 1) * $size . " , " . $size;
    $list = pdo_fetchall($sql);

    $total = pdo_fetchall("select id from " . tablename('tg_favorable') . " WHERE uniacid = {$_W['uniacid']} and isshow = 1");
    $total = count($total);

    $pager = pagination($total, $page, $size);

    include wl_template('goods/coupon');
} elseif ($operation == 'off_shelves') {

    $size = 7;
    $page = !empty($_GPC['page']) ? $_GPC['page'] : 1;
    $sql = "select * from " . tablename('tg_favorable') . " where uniacid='{$_W['uniacid']}' and isshow = 0 LIMIT " . ($page - 1) * $size . " , " . $size;
    $list = pdo_fetchall($sql);

    $total = pdo_fetchall("select id from " . tablename('tg_favorable') . " WHERE uniacid = {$_W['uniacid']} and isshow = 0");
    $total = count($total);

    $pager = pagination($total, $page, $size);

    include wl_template('goods/coupon');
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $_W['uniacid'],
            'name' => $_GPC['name'],
            'subtitle' => $_GPC['subtitle'],
            'applicant' => $_GPC['applicant'],
            'up_limit' => $_GPC['up_limit'],
            'deadline' => strtotime($_GPC['deadline']),
            'pic' => $_GPC['pic'],
            'start_time' => strtotime($_GPC['start_time']),
            'end_time' => strtotime($_GPC['end_time']),
            'class_time' => $_GPC['class_time'],
            'person' => $_GPC['person'],
            'course' => $_GPC['course'],
            'address' => $_GPC['address'],
            'term' => $_GPC['term'],
            'salesman' => $_GPC['salesman'],
            'times ' => $_GPC['times']
        );
        if (!empty($id)) {
            pdo_update('tg_favorable', $data, array('id' => $id));
            message('更新优惠券成功！', web_url('goods/coupon', array('op' => 'display')), 'success');
        } else {
            pdo_insert('tg_favorable', $data);
            $id = pdo_insertid();
            message('添加优惠券成功！', web_url('goods/coupon', array('op' => 'display')), 'success');
        }

    }
    $coupon = pdo_fetch("select * from " . tablename('tg_favorable') . " where id=:id and uniacid=:uniacid limit 1", array(":id" => $id, ":uniacid" => $_W['uniacid']));
    include wl_template('goods/coupon');
} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $coupon = pdo_fetch("SELECT id FROM " . tablename('tg_favorable') . " WHERE id = '$id' AND uniacid=" . $_W['uniacid'] . "");
    if (empty($coupon)) {
        message('抱歉，优惠券不存在或是已经被删除！', web_url('goods/coupon', array('op' => 'display')), 'error');
    }
    pdo_delete('tg_favorable', array('id' => $id));
    message('优惠券删除成功！', web_url('goods/coupon', array('op' => 'display')), 'success');
} elseif ($op == 'batch') {
    $funcop = $_GPC['funcop'];
    $goods_ids = $_GPC['goods_ids'];
    foreach ($goods_ids as $key => $id) {
        if ($funcop == 'on_shelves') {
            if (pdo_update('tg_favorable', array('isshow' => 1), array('id' => $id))) {
            } else {
            }
        }
        if ($funcop == 'off_shelves') {
            if (pdo_update('tg_favorable', array('isshow' => 0), array('id' => $id))) {
            } else {
            }
        }
    }
    if ($funcop == 'on_shelves') {
        die(json_encode(array("errno" => 0, 'message' => '上架成功')));
    }
    if ($funcop == 'off_shelves') {
        die(json_encode(array("errno" => 0, 'message' => '下架成功')));
    }

} elseif ($op == 'single_op') {
    $funcop = $_GPC['funcop'];
    $id = $_GPC['id'];
    if ($funcop == 'on_shelves') {
        if (pdo_update('tg_favorable', array('isshow' => 1), array('id' => $id))) {
            die(json_encode(array("errno" => 0, 'message' => '上架成功')));
        } else {
            die(json_encode(array("errno" => 1, 'message' => '上架失败')));
        }
    }
    if ($funcop == 'off_shelves') {
        if (pdo_update('tg_favorable', array('isshow' => 0), array('id' => $id))) {
            die(json_encode(array("errno" => 0, 'message' => '下架成功')));
        } else {
            die(json_encode(array("errno" => 1, 'message' => '下架失败')));
        }
    }

} else {
    message('请求方式不存在');
}
exit();